package com.kaikeba.wx.controller;

import com.kaikeba.bean.BootStrapTableExpress;
import com.kaikeba.bean.Express;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.mvc.ResponseView;
import com.kaikeba.service.ExpressService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class QRCodeController {

    @ResponseView("/wx/qrcode.do")
    public String createQRCode(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");//code获取的值可能会为Null,因为有不同请求
        String type = request.getParameter("type");
//        System.out.println("type:"+type);
        String userPhone = null;
        String qRCodeContent = null;
        if(type.equals("express")){
            //单个快递的二维码
            //展示用户单个快递信息
            qRCodeContent = "express_"+code;

        }else if(type.equals("user")){
            //用户的专属的二维码，展示用户所有快递
            User wxUser = UserUtil.getWxUser(request.getSession());
            userPhone = wxUser.getPhoneNumber();
            qRCodeContent = "userPhone_"+userPhone;
        }
        HttpSession session = request.getSession();
        session.setAttribute("qRCode",qRCodeContent);
        return "/personQRcode.html";

    }

    @ResponseBody("/wx/getQRCode.do")
    public String getQRCode(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        String qRCode = (String) session.getAttribute("qRCode");
        Message msg = new Message();
        if(qRCode==null){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
            msg.setResult(qRCode);
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }

    @ResponseBody("/wx/updateStatus.do")
    public String updateExpressStatus(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        boolean flag = ExpressService.updateStatus(code);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("取件成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("取件失败，请用户更新二维码");
        }
        String s = JSONUtil.toJSON(msg);
        return s;
    }

    @ResponseBody("wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        Express e = ExpressService.findByCode(code);
        BootStrapTableExpress e2 = null;
        if(e!=null) {
            e2 = new BootStrapTableExpress(e.getId(), e.getNumber(),
                    e.getUserName(), e.getUserPhone(), e.getCompany(), e.getCode(),
                    DateFormatUtil.format(e.getInTime()), e.getOutTime() == null ? "未出库" : DateFormatUtil.format(e.getOutTime()),
                    e.getStatus() == 0 ? "未取件" : "已取件", e.getSysPhone());
        }

        Message msg = new Message();
        if(e ==null){
            msg.setStatus(-1);
            msg.setResult("查询不到快递");
        }else{
            msg.setStatus(0);
            msg.setResult("成功查询到快递");
            msg.setData(e2);
        }
        String s = JSONUtil.toJSON(msg);
        return s;

    }
}
